<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑通讯录联系人</title>

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- bootstrap-validate -->
    <link rel="stylesheet" href="./assets/plugins/bootstrap-validator/bootstrapValidator.min.css" />

    <!-- mui -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css" />

    <!-- city -->
    <link rel="stylesheet" href="./assets/plugins/city-picker/city-picker.css" />

    <!-- 重置样式 -->
    <link rel="stylesheet/less" href="./assets/css/reset.less" />

    <!-- 公共样式 -->
    <link rel="stylesheet/less" href="./assets/css/common.less" />

    <!-- 页面独立的 -->
    <link rel="stylesheet/less" href="./assets/css/ContactAdd.less" />
</head>

<body>
    <!-- 信息编辑 -->
    <div class="info">
        <h3 class="title wow fadeInUp">编辑通讯录联系人</h3>

        <p class="desc wow fadeInUp" data-wow-delay="200ms">
            您可在本页编辑您的通讯录联系人，编辑成功后通讯录联系人更新后的信息将会出现在首页。
        </p>
    </div>

    <form id="edit" class="contact wow fadeInUp" data-wow-delay="300ms">
        <div class="form-group">
            <label for="cardid">通讯录ID</label>
            <input type="text" class="form-control" name="cardid" id="cardid" placeholder="通讯录ID" />
        </div>

        <div class="form-group">
            <label for="businessid">会员ID</label>
            <input type="text" class="form-control" name="businessid" id="businessid" placeholder="会员ID" />
        </div>

        <div class="form-group">
            <label for="nickname">昵称</label>
            <input type="text" class="form-control" name="nickname" id="nickname" placeholder="昵称" />
        </div>

        <div class="form-group">
            <label for="mobile">手机号</label>
            <input type="text" class="form-control" name="mobile" id="mobile" placeholder="手机号" />
        </div>

        <div class="form-group">
            <label for="gender">性别</label>
            <select id="gender" class="form-control">
                <option value="0">保密</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>

        <div class="form-group">
            <label for="typeid">分类ID</label>
            <input type="text" class="form-control" name="typeid" id="typeid" placeholder="分类ID" />
        </div>

        <div class="form-group" style="position: relative;">
            <label for="region">地区</label>
            <input type="text" class="form-control" readonly data-toggle="city-picker" name="region" id="region"
                placeholder="请选择地区" />
        </div>

        <div class="form-group">
            <label for="remark">备注信息</label>
            <input type="text" class="form-control" name="remark" id="remark" placeholder="备注信息" />
        </div>

        <div class="action">
            <button class="btn">提交</button>
        </div>
    </form>
</body>

</html>
<!-- 解析less的文件 -->
<script src="./assets/js/less.min.js"></script>

<!-- jquery  -->
<script src="./assets/js/jquery.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- bootstrap-validate -->
<script src="./assets/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>

<!-- city -->
<script src="./assets/plugins/city-picker/city-picker.data.min.js"></script>
<script src="./assets/plugins/city-picker/city-picker.min.js"></script>


<!-- mui -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- axios -->
<script src="./assets/plugins/axios/axios.min.js"></script>

<!-- 服务请求 -->
<script src="./assets/plugins/axios/request.js"></script>

<!-- cookie -->
<script src="./assets/plugins/cookie.js"></script>

<!-- helpers -->
<script src="./assets/plugins/helpers.js"></script>

<script>
    //实例化wow.js
    new WOW().init();

    // 获取ID
    let id = getParam('id');

    let business = {};

    (async () => {
        let result = await check(true, true);

        business = result.data;

        getContactInfo();
    })();

    // 回显 获取通讯录联系人详情数据
    async function getContactInfo() {
        let params = {
            businessid: business.id,
            cardid: id
        }

        let result = await POST({
            url: '/card//card/check',
            params
        })
        
        if (result.code === 1) {
            let info = result.data;

            $('#cardid').val(info.id);
            $('#businessid').val(business.id)
            $('#nickname').val(info.nickname);
            $('#mobile').val(info.mobile);
            $('#gender').val(info.gender);
            $('#typeid').val(info.typeid)
            $('#remark').val(info.remark);

            $('#region').citypicker('reset');
            $('#region').citypicker('destroy');

            $('#region').citypicker({
                province: info.province_text,
                city: info.city_text,
                district: info.district_text
            });



            return false;
        } else {
            mui.toast(result.msg, { duration: 1500 });

            setTimeout(() => {
                location.href = 'index.html';
            }, 1500);

            return false;
        }
    }

    // 实例化验证器
    $('#edit').bootstrapValidator({
        // 填写配置项
        message: '表单内容不能为空',// 所有字段默认错误信息
        // 字段校验图标。默认bootstrap图标
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            cardid: {
                validators: {
                    // 必填
                    notEmpty: {
                        message: '通讯录ID不能为空'
                    }
                }
            },
            businessid: {
                validators: {
                    // 必填
                    notEmpty: {
                        message: '会员ID不能为空'
                    }
                }
            },
            gender: {
                validators: {
                    // 必填
                    notEmpty: {
                        message: '通讯录ID不能为空'
                    },
                     // 校验字段长度
                     stringLength: {
                        min: 11,// 最小长度
                        max: 11,// 最大长度
                        message: '手机号必须是11位'
                    },
                    // 正则校验
                    regexp: {
                        regexp: /^1[3-9]\d{9}$/,
                        message: '手机号格式错误'
                    }
                }
            },
            typeid:{
                validators:{
                    // 必填
                    notEmpty: {
                        message: '类型ID不能为空'
                    }
                }
            },
            mobile: {
                validators: {
                    // 必填
                    notEmpty: {
                        message: '手机号不能为空'
                    }
                }
            },
            nickname: {
                validators: {
                    // 必填
                    notEmpty: {
                        message: '昵称不能为空'
                    }
                }
            },
            region: {
                validators: {
                    // 必填
                    notEmpty: {
                        message: '地区码不能为空'
                    }
                }
            } ,

        }
    }).on('success.form.bv', function (e) {
        // 阻止多次提交
        e.preventDefault();
    });

    // 表单提交
    $('#edit').on('submit', async function () {
        // 判断表单验证是否失败
        if (!$('#edit').data('bootstrapValidator').isValid()) {

            // 验证没有通过就不执行后面代码
            return false;
        }

        // 组装数据
        let params = {
            cardid: $.trim($('#cardid').val()),
            businessid:business.id,
            nickname: $.trim($('#nickname').val()),
            mobile: $.trim($('#mobile').val()),
            gender: $.trim($('#gender').val()),
            typeid: $.trim($('#typeid').val()),
            remark: $.trim($('#remark').val()),
            region: $.trim($('#region').val()),

        }

        let result = await POST({
            url: '/card/card/edit',
            params
        })
        
        if (result.code === 1) {
            mui.toast(result.msg, { duration: 1500 });

            setTimeout(() => {
                location.href = 'Index.html';
            }, 1500);

            return false;
        } else {
            mui.toast(result.msg, { duration: 1500 });

            return false;
        }
    })

</script>